﻿using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using K9Nano.Application.Dtos;
using K9Nano.Domain.Entities;
using K9Nano.GlobalOptions;

#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
namespace K9Nano.Application.Authentication;
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配

public class RoleDto : AuditDto
{
    public string Code { get; set; }
    public string Name { get; set; }
}


public class RoleEditDto : RoleCreateDto, IKeyDto
{
    [Required]
    public long Id { get; set; }
}


public class RoleCreateDto
{
    [StringLength(50)]
    [RegularExpression(RegexPatterns.EnDigitUnderline, ErrorMessage = RegexPatterns.EnDigitUnderlineError)]
    public string Code { get; set; }

    [StringLength(50)]
    [RegularExpression(RegexPatterns.ChEnDigitUnderline, ErrorMessage = RegexPatterns.ChEnDigitUnderlineError)]
    public string Name { get; set; }

    public string[] Permissions { get; set; }
}

public class RoleQueryDto<TRole> : PagedQueryDto<TRole>
    where TRole : RoleBase
{
    public string? Code { get; set; }
    public string? Name { get; set; }

    public override Expression<Func<TRole, bool>>? GetPredicate()
    {
        if (string.IsNullOrWhiteSpace(Code) && string.IsNullOrWhiteSpace(Name))
        {
            return null;
        }

        if (!string.IsNullOrWhiteSpace(Name))
        {
            return a => a.Name.Contains(Name);
        }

        return a => a.Code.Contains(Code!);
    }
}

public class RolePermissionEditDto
{
    public long RoleId { get; set; }
    public string[] Permissions { get; set; }
}
